package cn.iocoder.yudao.module.jikong.controller.admin.address;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.jikong.controller.admin.address.vo.*;
import cn.iocoder.yudao.module.jikong.dal.dataobject.address.AddressDO;
import cn.iocoder.yudao.module.jikong.service.address.AddressService;

@Tag(name = "管理后台 - 地址管理")
@RestController
@RequestMapping("/jikong/address")
@Validated
public class AddressController {

    @Resource
    private AddressService addressService;

    @PostMapping("/create")
    @Operation(summary = "创建地址管理")
    @PreAuthorize("@ss.hasPermission('jikong:address:create')")
    public CommonResult<Long> createAddress(@Valid @RequestBody AddressSaveReqVO createReqVO) {
        return success(addressService.createAddress(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新地址管理")
    @PreAuthorize("@ss.hasPermission('jikong:address:update')")
    public CommonResult<Boolean> updateAddress(@Valid @RequestBody AddressSaveReqVO updateReqVO) {
        addressService.updateAddress(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除地址管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('jikong:address:delete')")
    public CommonResult<Boolean> deleteAddress(@RequestParam("id") Long id) {
        addressService.deleteAddress(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除地址管理")
                @PreAuthorize("@ss.hasPermission('jikong:address:delete')")
    public CommonResult<Boolean> deleteAddressList(@RequestParam("ids") List<Long> ids) {
        addressService.deleteAddressListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得地址管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('jikong:address:query')")
    public CommonResult<AddressRespVO> getAddress(@RequestParam("id") Long id) {
        AddressDO address = addressService.getAddress(id);
        return success(BeanUtils.toBean(address, AddressRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得地址管理分页")
    @PreAuthorize("@ss.hasPermission('jikong:address:query')")
    public CommonResult<PageResult<AddressRespVO>> getAddressPage(@Valid AddressPageReqVO pageReqVO) {
        PageResult<AddressDO> pageResult = addressService.getAddressPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AddressRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出地址管理 Excel")
    @PreAuthorize("@ss.hasPermission('jikong:address:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAddressExcel(@Valid AddressPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AddressDO> list = addressService.getAddressPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "地址管理.xls", "数据", AddressRespVO.class,
                        BeanUtils.toBean(list, AddressRespVO.class));
    }

}